
import math

spO2LUT = [100,100,100,100,
           99,99,99,99,99,99,
           98,98,98,98,98,
           97,97,97,97,97,97,
           96,96,96,96,96,96,
           95,95,95,95,95,95,
           94,94,94,94,94,
           93,93,93,93,93]
class SpO2Calculator():

    def __init__(self):
        self.irACValueSqSum = 0
        self.redACValueSqSum = 0
        self.beatsDetectedNum = 0
        self.samplesRecorded = 0
        self.spO2 = 0


    def SPO2update(self,irACValue,redACValue):
        self.irACValueSqSum+=irACValue*irACValue
        self.redACValueSqSum+=redACValue*redACValue
        self.samplesRecorded+=1

        # if(beatDetected):
        try:

            acsqRatio = 100.0 *math.log(self.redACValueSqSum/self.samplesRecorded)/math.log(self.irACValueSqSum/self.samplesRecorded)
            index = 0
            if(acsqRatio>66):
                index=acsqRatio-66
            elif(acsqRatio>50):
                index=acsqRatio-50
        except :
            index = 0


        print(index)
        self.spO2=spO2LUT[int(index)]



    def reset(self):
        self.irACValueSqSum = 0
        self.redACValueSqSum = 0
        self.beatsDetectedNum = 0
        self.samplesRecorded = 0
        self.spO2 = 0
    def get_spo2(self):
        return self.spO2
